package com.frank.im.utils;

import android.content.Context;

import com.frank.http.encryp.EncrypUtils;
import com.frank.http.expand.PreferenceAppService;
import com.frank.im.base.ImException;
import com.frank.im.bean.ImReqData;

/**
 * im数据转换类
 * 
 * @author Frank
 * @version 1.0 Create by 2015.4.8
 */
public class ImEncrypUtils {
	private final static String TAG = ImEncrypUtils.class.getSimpleName();
	public static final String SESSION_ID = "session_id";
	public static final String SESSION_CARD = "session_card";
	public static final String SESSION_KEY = "session_key";
	public static final String SESSION_SIGN = "session_sign";
	public static final String SESSION_ID_NAME = "session_id_name";
	public static final String SESSION_CARD_NAME = "session_card_name";
	public static final String SESSION_KEY_NAME = "session_key_name";
	public static final String SESSION_SIGN_NAME = "session_sign_name";
	public static final String SESSION_HEADERS_PREFIX = "session_prefix";
	public static final String SESSION_URL = "session_url";

	/**
	 * 获得初始化请求数据
	 * 
	 * @param context
	 * @param requestId
	 * @return
	 */
	public static ImReqData getInitData(Context context, String requestId) {
		return getRequest(context, requestId, "init" ,"");
	}

	/**
	 * 获得请求数据内容
	 * 
	 * @param context
	 * @param requestId
	 * @param data
	 * @return
	 */
	public static ImReqData getSendContent(Context context, String requestId,
			String type,String content) {
		if (content == null) {
			try {
				throw new ImException("content==null 请求不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		return getRequest(context, requestId,
				type,content);
	}

	/**
	 * 获得基础请求数据
	 */
	private static ImReqData getRequest(Context context, String requestId,
			String type,String data) {
		ImReqData reqData = new ImReqData();
		reqData.setPath(type);
		reqData.setData(data == null ? "" : data);
		reqData.setType("user");
		String session_request_id = requestId;
		String session_id = PreferenceAppService.getInstance().getItem(
				SESSION_ID);
		String session_card = PreferenceAppService.getInstance()
				.getItem(SESSION_CARD).equals("0") ? EncrypUtils
				.getUid(context) : PreferenceAppService.getInstance().getItem(
				SESSION_CARD);
		String session_key = PreferenceAppService.getInstance()
				.getItem(SESSION_KEY).equals("0") ? "session_key"
				: PreferenceAppService.getInstance().getItem(SESSION_KEY);
		String sign_utc_time = EncrypUtils.getUTC();
		String expired_time_offset = "1800";
		String auth_string = EncrypUtils.getAuthString("app-ws-auth-v1",
				session_request_id, session_id, session_card, sign_utc_time,
				expired_time_offset);
		String signing_key = EncrypUtils
				.getSigningKey(auth_string, session_key);
		String data_md5_raw_base64 = EncrypUtils.get128MD5ToBase64(reqData
				.getData());
		String sign_string = EncrypUtils.getAuthString(auth_string,
				reqData.getType(), reqData.getPath(), data_md5_raw_base64);
		String session_sign = EncrypUtils.getSigningKey(sign_string,
				signing_key);
		auth_string = EncrypUtils.getAuthString(auth_string, session_sign);
		reqData.setRequest_id(session_request_id);
		reqData.setAuthorization(auth_string);
		return reqData;
	}

}
